#!/bin/sh

test_description="test proc-receive"

. ./lib/sharness.sh

REPO_ID=""

#创建repo
test_expect_success "[test-1] init repo and branch" '
  REPO_ID=$(find_repo | jq .id)
  if [ -n $REPO_ID ]; then
    delete_repo > /dev/null 2>&1
    test_expect_code 0
  fi

  REPO_ID=$(create_repo) &&
  add_member $WRITE_LEVEL $another_user_external &&
  init_repo
  test_expect_code 0
'

#创建 master 分支
test_expect_success "[test-2] normal push success" '
  echo "This is the first commit" > "$REPO_DIR"/README &&
  git -C "$REPO_DIR" add README &&
  git -C "$REPO_DIR" commit -m "first commit" --author "yunhuai-test <yunhuai.xzy@alibaba-inc.com>" --date=now >/dev/null 2>&1 &&
  git -C "$REPO_DIR" push origin master > test-2 2>&1
'

#new模式创建mr成功
test_expect_success "[test-3][new] creating new mr success" '
  add_member $READ_LEVEL $another_user_external &&
  echo "[test-3][new] creating new mr success" >> "$REPO_DIR"/test.file &&
  git -C "$REPO_DIR" add test.file &&
  git -C "$REPO_DIR" commit -m "creating new mr success" --author "yunhuai-test <yunhuai.xzy@alibaba-inc.com>" --date=now >/dev/null 2>&1 &&
  git -C "$REPO_DIR" push origin master -o review=new > test-3 2>&1 &&
  grep "has been created" test-3
'

test_expect_success "[test-4][mrid] author update mr success" '
  mr_id=1
  echo "[test-4][mrid] author update mr success" >> "$REPO_DIR"/test.file &&
  git -C "$REPO_DIR" add test.file &&
  git -C "$REPO_DIR" commit -m "author update mr success" --author "yunhuai-test <yunhuai.xzy@alibaba-inc.com>" --date=now >/dev/null 2>&1 &&
  git -C "$REPO_DIR" push origin master -o review=$mr_id > test-4 2>&1 &&
  grep "has been updated" test-4
'

test_expect_success "[test-5][mrid][diverge] local branch diverged, single user update mr success" '
  mr_id=1
  echo "[test-5][mrid][diverge] local branch diverged, single user update mr success" >> "$REPO_DIR"/test.file &&
  git -C "$REPO_DIR" add test.file &&
  git -C "$REPO_DIR" commit --amend -m "local branch diverged, single user update mr success" --author "yunhuai-test <yunhuai.xzy@alibaba-inc.com>" --date=now >/dev/null 2>&1 &&
  git -C "$REPO_DIR" push origin master -o review=$mr_id > test-5 2>&1 &&
  grep "has been updated" test-5
'

#主干模式暂不支持指定reviewer
test_expect_success "[test-6][mrid] neither author or reviewer, update mr fail" '
  git clone https://"$user_http_name":"$user_http_password"@"$clone_domain"/"$tenant_id"/$REPO_NAME.git "$REVIEWER_REPO_NAME" &&
  mr_id=1
  git -C "$REVIEWER_REPO_DIR" fetch origin "+refs/changes/*:refs/remotes/origin/changes/*" &&
  git -C "$REVIEWER_REPO_DIR" checkout -b review_"$mr_id" origin/changes/"$mr_id"/head &&
  echo "[test-6][mrid] neither author or reviewer, update mr fail" >> "$REVIEWER_REPO_DIR"/test.file &&
  git -C "$REVIEWER_REPO_DIR" add test.file &&
  git -C "$REVIEWER_REPO_DIR" commit -m "neither author or reviewer, update mr fail" --author "yunhuai-test <yunhuai.xzy@alibaba-inc.com>" --date=now >/dev/null 2>&1 &&
  test_must_fail git -C "$REVIEWER_REPO_DIR" push -o review=$mr_id origin HEAD:refs/changes/"$mr_id"/head > test-6 2>&1 &&
  grep "failed to push some refs" test-6
'

test_expect_success "[test-7][mrid][diverge] local branch diverged, reviewer update mr fail" '
  mr_id=1
  add_reviewer $REPO_ID $mr_id $user_id &&
  echo "review edit" >> "$REVIEWER_REPO_DIR"/test.file &&
  git -C "$REVIEWER_REPO_DIR" add test.file &&
  git -C "$REVIEWER_REPO_DIR" commit -m "local branch diverged, reviewer update mr fail" --author "yunhuai-test <yunhuai.xzy@alibaba-inc.com>" --date=now >/dev/null 2>&1 &&
  echo "author update mr to create diverge" >> "$REPO_DIR"/test.file &&
  git -C "$REPO_DIR" add test.file &&
  old_oid=$(git -C "$REPO_DIR" ls-remote origin refs/changes/$mr_id/head 2>&1 | awk "{print \$1}") &&
  git -C "$REPO_DIR" commit -m "author update mr to create diverge" --author "yunhuai-test <yunhuai.xzy@alibaba-inc.com>" --date=now >/dev/null 2>&1 &&
  git -C "$REPO_DIR" push origin master -o review="$mr_id" -o old-oid="$old_oid" > test-7-1 2>&1 &&
  test_must_fail git -C "$REVIEWER_REPO_DIR" push -o review="$mr_id" origin HEAD:refs/changes/"$mr_id"/head > test-7-2 2>&1 &&
  grep "failed to push some refs" test-7-2
'

test_expect_success "[test-8][mrid] reviewer update mr success" '
  mr_id=1
  add_reviewer $REPO_ID $mr_id $user_id &&
  git -C "$REVIEWER_REPO_DIR" fetch origin "+refs/changes/*:refs/remotes/origin/changes/*" &&
  git -C "$REVIEWER_REPO_DIR" reset --hard origin/changes/"$mr_id"/head &&
  echo "[test-8][mrid] reviewer update mr success" >> "$REVIEWER_REPO_DIR"/test.file &&
  git -C "$REVIEWER_REPO_DIR" add test.file &&
  git -C "$REVIEWER_REPO_DIR" commit -m "reviewer update mr success" --author "yunhuai-test <yunhuai.xzy@alibaba-inc.com>" --date=now >/dev/null 2>&1 &&
  git -C "$REVIEWER_REPO_DIR" push -o review=$mr_id origin HEAD:master > test-8 2>&1 &&
  grep "has been updated" test-8
'

#以下针对保护分支的设置
#new模式push保护分支创建mr成功
test_expect_success "[test-9][new][read] read permission push protected branch creating new mr success" '
  mr_id=1
  close_merge_request $mr_id &&
  add_member $READ_LEVEL $another_user_external &&
  protect_branch master &&
  echo "[test-9][new] read permission push protected branch creating new mr success" >> "$REPO_DIR"/test.file &&
  git -C "$REPO_DIR" add test.file &&
  git -C "$REPO_DIR" commit -m "read permission push protected branch creating new mr success" --author "yunhuai-test <yunhuai.xzy@alibaba-inc.com>" --date=now >/dev/null 2>&1 &&
  git -C "$REPO_DIR" push origin master -o review=new > test-9 2>&1 &&
  grep "has been created" test-9
'

#作者更新保护分支mr成功
test_expect_success "[test-10][mrid] author update protected branch mr success" '
  mr_id=2
  echo "[test-10][mrid] author update protected branch mr success" >> "$REPO_DIR"/test.file &&
  git -C "$REPO_DIR" add test.file &&
  git -C "$REPO_DIR" commit -m "author update protected branch mr success" --author "yunhuai-test <yunhuai.xzy@alibaba-inc.com>" --date=now >/dev/null 2>&1 &&
  git -C "$REPO_DIR" push origin master -o review=$mr_id > test-10 2>&1 &&
  grep "has been updated" test-10
'

#评审者更新保护分支mr成功
test_expect_success "[test-11][mrid] reviewer update protected branch mr success" '
  mr_id=2
  add_reviewer $REPO_ID $mr_id $user_id &&
  git -C "$REVIEWER_REPO_DIR" fetch origin "+refs/changes/*:refs/remotes/origin/changes/*" &&
  git -C "$REVIEWER_REPO_DIR" reset --hard origin/changes/"$mr_id"/head &&
  echo "[test-11][mrid] reviewer update protected branch mr success" >> "$REVIEWER_REPO_DIR"/test.file &&
  git -C "$REVIEWER_REPO_DIR" add test.file &&
  git -C "$REVIEWER_REPO_DIR" commit -m "reviewer update protected branch mr success" --author "yunhuai-test <yunhuai.xzy@alibaba-inc.com>" --date=now >/dev/null 2>&1 &&
  git -C "$REVIEWER_REPO_DIR" push -o review=$mr_id origin HEAD:master > test-11 2>&1 &&
  grep "has been updated" test-11
'

test_expect_success "[test-12] clean up" '
  delete_repo "$REPO_ID" > test-12 2>&1
'

test_done